package cn.huiyunche.admin.tms.controller;

import cn.huiyunche.admin.service.DOilPriceService;
import cn.huiyunche.base.service.enums.OilProductsEnum;
import cn.huiyunche.base.service.model.DOilPrice;
import cn.huiyunche.base.service.vo.DOilPriceVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jxy on 17/1/5.
 */
@Controller
@RequestMapping("/doilprice")
public class DOilPriceController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DOilPriceController.class);

    @Autowired
    private DOilPriceService dOilPriceService;

    /**
     * 查询油价列表
     *
     * @param pageVo
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Result<Object> list(PageVo pageVo, DOilPriceVo dOilPriceVo) throws BusinessException {
        LOGGER.info("list params PageVo: {};dOilPriceVo:{}", pageVo,dOilPriceVo);
        Result<Object> result = new Result<>(true, null, "数据加载成功");
        result.setData(dOilPriceService.getListByPage(pageVo,dOilPriceVo));
        return result;
    }

    /**
     * 添加油价
     * @param dOilPrice 油价实体
     * @return 结果集
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> add(@Valid DOilPrice dOilPrice, BindingResult br) throws BusinessException {
        if(br.hasErrors()){
            String errorMessage = br.getAllErrors().get(0).getDefaultMessage();
            LOGGER.info("add method has error : "+ errorMessage);
            throw new IllegalArgumentException(errorMessage);
        }
        LOGGER.info("add params dOilPrice: {}",dOilPrice);
        //判断折扣在 0.01～1之间
        if (dOilPrice.getSeal() != null){
            if (dOilPrice.getSeal().compareTo(new BigDecimal(0)) != 1 || dOilPrice.getSeal().compareTo(new BigDecimal(1)) > 0 ){
                throw new BusinessException("折扣必须介于 0.01～1之间");
            }
        }
        dOilPriceService.addDOilPrice(dOilPrice);
        Result<Object> result = new Result<>(true,null,"添加油价表数据成功");
        return result;
    }

    /**
     * 设置可用不可用
     * @param id 油价id
     * @return 结果集
     */
    @RequestMapping(value = "/isenable", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> isEnable(Long id,Boolean isEnable) throws BusinessException {
        if(id == null || id == 0l){
            throw new BusinessException("id 不能为空");
        }
        LOGGER.info("isEnable params id: {};isEnable:{}",id,isEnable);
        dOilPriceService.isEnable(id,isEnable);
        Result<Object> result = new Result<>(true,null,"设置可用不可用数据成功");
        return result;
    }

    /**
     * 加载油品枚举
     * @return 结果集
     */
    @RequestMapping(value = "/loadoilproducts", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> loadOilProducts() throws BusinessException {
        Result<Object> result = new Result<>(true,null,"加载油品枚举数据成功");
        List<Map> res = new ArrayList<>();
        for (OilProductsEnum e : OilProductsEnum.values()) {
            Map temp = new HashMap();
            temp.put("code",e.getValue());
            temp.put("name",e.getText());
            res.add(temp);
        }
        result.setData(res);
        return result;
    }









}
